package com.itheima.bos.web.action.base;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.itheima.bos.bean.base.Courier;
import com.itheima.bos.bean.base.Standard;
import com.itheima.bos.service.CourierService;
import com.opensymphony.xwork2.ActionContext;


@Controller
@Namespace("/courier")
@Actions
@ParentPackage("json-default")
@Scope("prototype")
public class CourierAction extends BaseAction<Courier> {


	@Autowired
	private CourierService service;

	@Action(value = "courier_save", results = {
			@Result(name = "success", type = "redirect", location = "../pages/base/courier.html") })
	public String courier_save() {
		service.save(model);
		return SUCCESS;
	}


	@Action(value = "courier_pageQuery", results = { @Result(name = "success", type = "json") })
	public String courier_pageQuery() {
		System.out.println(model.getCourierNum());
		Pageable pageable = new PageRequest(page - 1, rows);
		//Page<Courier> pageData = service.findPageData(pageable);
	
		Specification<Courier> specification = new Specification<Courier>() {
	
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate = null;
				List<Predicate> list = new ArrayList<Predicate>();
				if (StringUtils.isNoneBlank(model.getCourierNum())) {
					Predicate p = cb.equal(root.get("courierNum").as(String.class), model.getCourierNum());
					list.add(p);
				}
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p = cb.like(root.get("company").as(String.class), "%" + model.getCompany() + "%");
					list.add(p);
				}
				if (StringUtils.isNotBlank(model.getType())) {
					Predicate p = cb.like(root.get("type").as(String.class), "%" + model.getType() + "%");
					list.add(p);
				}
				Join<Courier, Standard> join = root.join("standard", JoinType.INNER);
				if (model.getStandard() != null && model.getStandard().getId() != null
						&& StringUtils.isNotBlank(model.getStandard().getId().toString())) {
					Predicate p = cb.equal(join.get("id").as(Integer.class), model.getStandard().getId());
					list.add(p);
				}
	
				if (list.size() > 0 && list != null) {
					Predicate[] p = new Predicate[list.size()];
					predicate = cb.and(list.toArray(p));
				}
				return predicate;
			}
		};
	
		Page<Courier> page = service.findPageQueryCourierByCondition(specification, pageable);
		pushPageDataToValueStack(page);
	
		return SUCCESS;
	}

	private String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}

	@Action(value = "courier_delBatch", results = {
			@Result(name = "success", type = "redirect", location = "../pages/base/courier.html") })
	public String delBatch() {
		String[] idsArr = ids.split(",");
		service.delBatch(idsArr);
		return SUCCESS;
	}

	@Action(value = "courier_restoreBatch", results = {
			@Result(name = "success", type = "redirect", location = "../pages/base/courier.html") })
	public String restoreBatch() {
		String[] idsArr = ids.split(",");
		service.restoreBatch(idsArr);
		return SUCCESS;
	}

	@Action(value = "courier_delBatch2", results = {
			@Result(name = "success", type = "json") })
	public String delBatch2() {
		String[] idsArr = ids.split(",");
		service.delBatch(idsArr);
		List<Courier> list = service.findAll();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}

	@Action(value = "courier_restoreBatch2", results = {
			@Result(name = "success", type = "json") })
	public String restoreBatch2() {
		String[] idsArr = ids.split(",");
		service.restoreBatch(idsArr);
		List<Courier> list = service.findAll();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}

}
